﻿using System;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using Entities = Vision.Core.Entities;
using Components = Vision.Core.Components;

namespace Vision.Web.Pages
{
    public partial class ActivityLog : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack){
                this.txtStartDate.Text = DateTime.Now.Date.ToShortDateString();
                this.txtEndDate.Text = DateTime.Now.Date.ToShortDateString();

                int activityId = int.Parse(Request.QueryString["ActivityId"]);

                using (Components.ActivityCatalog activityCatalog = new Components.ActivityCatalog(ConfigurationManager.AppSettings["VisionCatalog"])){
                    Entities.Activities.VisionActivity activity = activityCatalog.GetVisionActivityFromCatalog(activityId);
                    this.lblActivtyName.Text = activity.Name;
                    this.lblActivtyDesc.Text = activity.Description;
                    this.lblActivtyName.DataBind();                    
                    this.lblActivtyDesc.DataBind();
                }            
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            DateTime startDate = DateTime.Parse(this.txtStartDate.Text);
            DateTime endDate = DateTime.Parse(this.txtEndDate.Text).AddDays(1).AddTicks(-1);
            int activityId = int.Parse(Request.QueryString["ActivityId"]);

            using (Components.ActivityCatalog activityCatalog = new Components.ActivityCatalog(ConfigurationManager.AppSettings["VisionCatalog"])){
                this.gvActivityLog.DataSource = activityCatalog.GetActivityHistoryItems(activityId, startDate, endDate).OrderBy(hi => hi.Date);
                this.gvActivityLog.DataBind();
            }            
        }
                
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Activities.aspx");
        }        
    }
}